//
//  BookManagerViewModel.swift
//  KoiAccounting
//
//  Created by 纪洪文 on 03.04.25.
//

import Foundation
class BookManagerViewModel : KoiAccountingBaseViewModel {
    
    private let bookDao = KoiAccountingDB.standard.getBookDao()
    
    @Published var books = Array<AccountingBook>()
    
    @Published var archiveState = ""
    @Published var deleteState = ""
    
    func loadBooks() {
        asyncBackground {
            let result = self.bookDao.getAllBook(uid: self.uid)
            self.asyncMain {
                self.books = result
            }
        }
    }
    
    func archive(book: AccountingBook?) {
        guard let book = book else { return }
        asyncBackground {
            self.bookDao.archive(book: book)
            self.asyncMain {
                self.archiveState = "success"
            }
        }
    }
    
    func delete(book: AccountingBook?) {
        guard let book = book else { return }
        asyncBackground {
            self.bookDao.delete(book: book)
            self.asyncMain {
                self.deleteState = "success"
            }
        }
    }
}
